using System;
using UnityEngine;

public class HeliController : MonoBehaviour
{
	private GameObject _heliPivot;

	public bool EnableControls;

	public bool MouseControl;

	public bool IsFlyingDown;

	public HeliSounds helisound1;

	public HeliSounds helisound2;

	private bool isFlying;

	public float MaxHeight;

	public float MaxSpeed;

	public float MaxAngle;

	private float _minAngle;

	public float MaxEngineForce;

	[NonSerialized]
	public float CurEngineForce;

	public float Acceleration;

	private float _curAcceleration;

	public float RotationSpeed;

	public float TiltSpeed;

	public float StabilizationForce;

	private float[] _input = new float[4];

	private bool _anyInput;

	[NonSerialized]
	public bool HeliBladesHitting;

	private Vector3 _lastPos;

	private Rigidbody _rigidbody;

	private HelicopterBehavior helicopterBehavior;

	private UIJoystick joystick;

	private float _angVelocityBoost;

	private float _forceToTakeOff;

	private bool _takeOff;

	public float landDistance = 1f;

	public bool IsFlying
	{
		get
		{
			return isFlying;
		}
		set
		{
			isFlying = value;
		}
	}

	private void Awake()
	{
		_heliPivot = base.transform.Find("HeliPivot").gameObject;
		_minAngle = 360f - MaxAngle;
		_rigidbody = GetComponent<Rigidbody>();
		_rigidbody.centerOfMass = new Vector3(0f, -3f, 0f);
		Input.gyro.enabled = true;
	}

	private void Start()
	{
		helicopterBehavior = GetComponent<HelicopterBehavior>();
		joystick = FlyWindow.instance.joystick;
		if (!settings.offlineMode && base.transform.name == "Helicopter_military")
		{
			MaxHeight = 90f;
		}
	}

	private void OnDisable()
	{
	}

	private void Update()
	{
		if (!EnableControls)
		{
			return;
		}
		if (MouseControl)
		{
			if (Input.GetAxis("Mouse X") > 0f && Input.GetAxis("Mouse X") < 2f)
			{
				_input[0] = Input.GetAxis("Mouse X");
			}
			else if (Input.GetAxis("Mouse X") < 0f && Input.GetAxis("Mouse X") > -2f)
			{
				_input[1] = Mathf.Abs(Input.GetAxis("Mouse X"));
			}
			if (Input.GetAxis("Mouse X") == 0f)
			{
				_input[0] = 0f;
				_input[1] = 0f;
			}
			if (Input.GetAxis("Mouse Y") > 0f && Input.GetAxis("Mouse Y") < 2f)
			{
				_input[2] = Input.GetAxis("Mouse Y");
			}
			else if (Input.GetAxis("Mouse Y") < 0f && Input.GetAxis("Mouse Y") > -2f)
			{
				_input[3] = Mathf.Abs(Input.GetAxis("Mouse Y"));
			}
			if (Input.GetAxis("Mouse Y") == 0f)
			{
				_input[2] = 0f;
				_input[3] = 0f;
			}
		}
		else
		{
			if (Input.GetKey(KeyCode.Keypad6) || Input.GetKey(KeyCode.L) || Input.acceleration.x > 0.3f)
			{
				_input[0] = 1f;
			}
			else
			{
				_input[0] = 0f;
			}
			if (Input.GetKey(KeyCode.Keypad4) || Input.GetKey(KeyCode.J) || Input.acceleration.x < -0.3f)
			{
				_input[1] = 1f;
			}
			else
			{
				_input[1] = 0f;
			}
			if (Input.GetKey(KeyCode.Keypad8) || Input.GetKey(KeyCode.I) || joystick.position.y > 0.3f)
			{
				_input[2] = 1f;
			}
			else
			{
				_input[2] = 0f;
			}
			if (Input.GetKey(KeyCode.Keypad2) || Input.GetKey(KeyCode.K) || joystick.position.y < -0.3f)
			{
				_input[3] = 1f;
			}
			else
			{
				_input[3] = 0f;
			}
		}
		if (_input[0] > 0f || _input[1] > 0f || _input[2] > 0f || _input[3] > 0f)
		{
			_anyInput = true;
		}
		else
		{
			_anyInput = false;
		}
	}

	private void FixedUpdate()
	{
		EnableControls = !helicopterBehavior.isFree;
		_forceToTakeOff = MaxEngineForce * 0.3f;
		_takeOff = CurEngineForce > _forceToTakeOff;
		if (EnableControls && !helicopterBehavior.isDead)
		{
			if (!IsFlyingDown && !isFlying && CurEngineForce > MaxEngineForce * 0.3f)
			{
				CurEngineForce += Time.deltaTime * 0.6f;
				_rigidbody.AddForce(Vector3.up * (_rigidbody.mass * 9.75f));
			}
			if (Input.GetAxis("Vertical") == 0f && !isFlying && _takeOff)
			{
				if (CurEngineForce <= MaxEngineForce * 0.9f)
				{
					CurEngineForce += Time.deltaTime * (MaxEngineForce * 0.9f);
				}
				else
				{
					CurEngineForce -= Time.deltaTime * (MaxEngineForce * 0.9f);
				}
			}
			if (Input.GetAxis("Vertical") == 1f || isFlying)
			{
				CurEngineForce += Time.deltaTime * (MaxEngineForce * 0.8f);
				if (_takeOff)
				{
					float num = 0f;
					num = ((!(base.transform.position.y < _lastPos.y)) ? 0f : ((_lastPos.y - base.transform.position.y) * 5000f));
					_rigidbody.AddForce(Vector3.up * (CurEngineForce * 0.8f + num));
				}
			}
			CurEngineForce -= Time.deltaTime * 0.15f;
		}
		else
		{
			CurEngineForce -= Time.deltaTime * _forceToTakeOff;
		}
		if (CurEngineForce < 0f)
		{
			CurEngineForce = 0f;
		}
		if (CurEngineForce > MaxEngineForce)
		{
			CurEngineForce = MaxEngineForce;
		}
		_heliPivot.transform.position = base.transform.position;
		_heliPivot.transform.rotation = new Quaternion(0f, base.transform.rotation.y, 0f, _heliPivot.transform.rotation.w);
		if (_takeOff)
		{
			if (EnableControls)
			{
				if (Input.GetAxis("Horizontal") == 1f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, new Vector3(_rigidbody.angularVelocity.x, RotationSpeed, _rigidbody.angularVelocity.z), Time.deltaTime);
				}
				if (Input.GetAxis("Horizontal") == -1f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, new Vector3(_rigidbody.angularVelocity.x, 0f - RotationSpeed, _rigidbody.angularVelocity.z), Time.deltaTime);
				}
				if (Input.GetAxis("Horizontal") == 0f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, new Vector3(_rigidbody.angularVelocity.x, 0f, _rigidbody.angularVelocity.z), Time.deltaTime * StabilizationForce);
				}
				if (joystick.position.x > 0.1f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, new Vector3(_rigidbody.angularVelocity.x, RotationSpeed * joystick.position.x, _rigidbody.angularVelocity.z), Time.deltaTime);
				}
				else if (joystick.position.x < -0.1f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, new Vector3(_rigidbody.angularVelocity.x, RotationSpeed * joystick.position.x, _rigidbody.angularVelocity.z), Time.deltaTime);
				}
				else if (Mathf.Abs(joystick.position.x) < 0.01f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, new Vector3(_rigidbody.angularVelocity.x, 0f, _rigidbody.angularVelocity.z), Time.deltaTime * StabilizationForce);
				}
			}
			if (base.transform.localRotation.z < MaxAngle || base.transform.localEulerAngles.z > _minAngle)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, -base.transform.forward * TiltSpeed * _input[0], Time.deltaTime);
			}
			if (base.transform.localEulerAngles.z > _minAngle || base.transform.localEulerAngles.z < MaxAngle)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, base.transform.forward * TiltSpeed * _input[1], Time.deltaTime);
			}
			if (base.transform.localEulerAngles.x < MaxAngle || base.transform.localEulerAngles.x > _minAngle)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, base.transform.right * TiltSpeed * _input[2], Time.deltaTime);
			}
			if (base.transform.localEulerAngles.x > _minAngle || base.transform.localEulerAngles.x < MaxAngle)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, -base.transform.right * TiltSpeed * _input[3], Time.deltaTime);
			}
			if (_rigidbody.velocity.magnitude > MaxSpeed)
			{
				_rigidbody.velocity = _rigidbody.velocity.normalized * MaxSpeed;
			}
			if (base.transform.localEulerAngles.x <= 180f)
			{
				_rigidbody.AddRelativeForce(new Vector3(0f, 0f, base.transform.localEulerAngles.x * _curAcceleration));
			}
			else
			{
				_rigidbody.AddRelativeForce(new Vector3(0f, 0f, (0f - (360f - base.transform.localEulerAngles.x)) * _curAcceleration));
			}
			if (base.transform.localEulerAngles.z <= 180f)
			{
				_rigidbody.AddRelativeForce(new Vector3((0f - base.transform.localEulerAngles.z) * _curAcceleration, 0f, 0f));
			}
			else
			{
				_rigidbody.AddRelativeForce(new Vector3((360f - base.transform.localEulerAngles.z) * _curAcceleration, 0f, 0f));
			}
			if (base.transform.localEulerAngles.z < 100f && base.transform.localEulerAngles.z > 80f && _curAcceleration > 0f)
			{
				_curAcceleration -= Time.deltaTime * 10f;
			}
			if (base.transform.localEulerAngles.z < 280f && base.transform.localEulerAngles.z > 260f && _curAcceleration > 0f)
			{
				_curAcceleration -= Time.deltaTime * 10f;
			}
			if (_curAcceleration < Acceleration)
			{
				_curAcceleration += Time.deltaTime * 2f;
			}
			float num2 = base.transform.localEulerAngles.x * (StabilizationForce * 0.01f) * TiltSpeed;
			if (base.transform.localEulerAngles.x > MaxAngle && base.transform.localEulerAngles.x < 180f)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, -base.transform.right * num2, Time.deltaTime);
			}
			else if (base.transform.localEulerAngles.x < _minAngle && base.transform.localEulerAngles.x > 180f)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, base.transform.right * num2, Time.deltaTime);
			}
			num2 = base.transform.localEulerAngles.z * (StabilizationForce * 0.01f) * TiltSpeed;
			if (base.transform.localEulerAngles.z > MaxAngle && base.transform.localEulerAngles.z < 180f)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, -base.transform.forward * num2, Time.deltaTime);
			}
			else if (base.transform.localEulerAngles.z < _minAngle && base.transform.localEulerAngles.z > 180f)
			{
				_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, base.transform.forward * num2, Time.deltaTime);
			}
			if (_input[0] == 0f && _input[1] == 0f)
			{
				if (base.transform.localEulerAngles.z < 360f && base.transform.localEulerAngles.z > 180f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, base.transform.forward * ((360f - base.transform.localEulerAngles.z) * 0.1f), Time.deltaTime);
				}
				if (base.transform.localEulerAngles.z > 0f && base.transform.localEulerAngles.z < 180f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, -base.transform.forward * (base.transform.localEulerAngles.z * 0.1f), Time.deltaTime);
				}
			}
			if (_input[2] == 0f && _input[3] == 0f)
			{
				if (base.transform.localEulerAngles.x < 360f && base.transform.localEulerAngles.x > 180f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, base.transform.right * ((360f - base.transform.localEulerAngles.x) * 0.1f), Time.deltaTime);
				}
				if (base.transform.localEulerAngles.x > 0f && base.transform.localEulerAngles.x < 180f)
				{
					_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, -base.transform.right * (base.transform.localEulerAngles.x * 0.1f), Time.deltaTime);
				}
			}
			if (!_anyInput)
			{
				_rigidbody.velocity = Vector3.Lerp(_rigidbody.velocity, Vector3.zero, Time.deltaTime * 0.2f);
			}
		}
		if (base.transform.position.y > MaxHeight)
		{
			_rigidbody.velocity = Vector3.Lerp(_rigidbody.velocity, Vector3.down * (base.transform.position.y - MaxHeight), Time.deltaTime * 1f);
		}
		_lastPos = base.transform.position;
	}

	public void StartRotors()
	{
		helisound1.enabled = true;
		helisound2.enabled = true;
		HeliPropeller[] componentsInChildren = base.transform.GetComponentsInChildren<HeliPropeller>(true);
		HeliPropeller[] array = componentsInChildren;
		foreach (HeliPropeller heliPropeller in array)
		{
			heliPropeller.gameObject.SetActive(true);
			heliPropeller.StartRotors();
		}
	}

	public void StopRotors()
	{
		helisound1.enabled = false;
		helisound2.enabled = false;
		HeliPropeller[] componentsInChildren = base.transform.GetComponentsInChildren<HeliPropeller>();
		CurEngineForce = 0f;
		HeliPropeller[] array = componentsInChildren;
		foreach (HeliPropeller heliPropeller in array)
		{
			heliPropeller.StopRotors();
			heliPropeller.gameObject.SetActive(false);
		}
	}

	public void RotateAround(float force)
	{
		if (CurEngineForce > 0f)
		{
			_rigidbody.angularVelocity = Vector3.Lerp(_rigidbody.angularVelocity, new Vector3(_rigidbody.angularVelocity.x, (0f - force) * (CurEngineForce / MaxEngineForce), _rigidbody.angularVelocity.z), Time.deltaTime);
		}
	}

	public void EngineSlowDown(float value)
	{
		if (CurEngineForce > MaxEngineForce * 0.75f)
		{
			CurEngineForce -= Time.deltaTime * (MaxEngineForce * value);
		}
	}

	public float GetEngineForceValue()
	{
		return CurEngineForce / MaxEngineForce;
	}
}
